<template>
  <div class="view-entity-page">
    <div class="view-head">
      <div class="title">查看</div>
      <div>
        <!-- <el-button class="btn" size="mini" type="primary" @click="gotoEditor"
          >编辑</el-button
        > -->
        <el-button class="btn" size="mini" type="primary" @click="goBack"
          >返回</el-button
        >
      </div>
    </div>
    <div class="view-entity">
      <section class="entity-msg">
        <div class="header">
          <div class="msg">实体信息</div>
          <!-- <div class="edit-button" @click="gotoEditor">
                   <i class="el-icon-edit-outline edit-icon"></i>
                   <span style="
                    cursor: context-menu;
                    ">编辑</span>
               </div> -->
        </div>
        <el-form ref="form" :model="form" label-width="86px" class="content">
          <el-form-item label="所属类型：">
            <div class="content-item one-row-text">
              {{ this.form.ontologyName }}
            </div>
          </el-form-item>
          <el-form-item
            label="实体名称："
            :class="form.entityStatus == 1 ? 'img-item' : ''"
          >
            <div
              class="content-item one-row-text"
              v-if="form.entityStatus == 0"
            >
              <span class="content-style" v-if="form.entityStatus == 0">{{
                form.entityName
              }}</span>
            </div>
            <el-image
              v-if="form.entityStatus == 1"
              style="
                width: 72px;
                height: 72px;
                text-align: center;
                margin-bottom: 20px;
                border: 1px solid #dcdfe6;
              "
              :src="form.entityName"
              :preview-src-list="[form.entityName]"
              fit="fill"
            >
              <i
                slot="error"
                class="image-slot"
                style="color: rgb(220, 223, 230); font-size: 12px"
              >
                暂无图片
              </i>
            </el-image>
          </el-form-item>
          <el-form-item label="实体模态：">
            <div class="content-item one-row-text">
              {{ this.form.entityStatus == "1" ? "图片" : "文字" }}
            </div>
          </el-form-item>
          <el-form-item label="标识码：">
            <div class="content-item one-row-text">
              {{ this.form.targetNumber }}
            </div>
          </el-form-item>
          <el-form-item label="置信度：">
            <div class="content-item one-row-text">
              {{ this.form.confidence }}
            </div>
          </el-form-item>
          <el-form-item label="消歧标识：">
            <div class="content-item one-row-text">
              {{ this.form.disambiguationIdentify }}
            </div>
          </el-form-item>
          <el-form-item label="实体简介：" class="content-detail">
            <div style="padding-left: 8px">{{ this.form.details }}</div>
          </el-form-item>
          <el-form-item label="图片上传：" class="upload">
            <div class="">
              <img
                :src="form.fileId"
                v-if="form.fileId"
                alt=""
                style="width: 72px; height: 72px"
              />
              <div
                v-else
                style="
                  width: 72px;
                  height: 72px;
                  border: 1px solid rgb(220, 223, 230);
                  color: rgb(220, 223, 230);
                  text-align: center;
                  line-height: 72px;
                "
              >
                暂无图片
              </div>
            </div>
          </el-form-item>
        </el-form>
      </section>
      <section class="property-msg">
        <div class="msg">属性信息</div>
        <div v-if="commonPropertyList.length === 0" class="no-peoperty">
          暂无数据
        </div>
        <el-form
          ref="form"
          :model="form"
          label-width="280px"
          class="content"
          v-else
          label-position="left"
        >
          <el-form-item
            :label="`${item.propertyName}：`"
            v-for="(item, index) in commonPropertyList"
            :key="index"
          >
            <!-- 图片或文件 -->
            <div
              v-if="item.propertyType == 'image' || item.propertyType == 'file'"
              :style="{
                color: '#6f8def',
                cursor: 'pointer',
              }"
              @click="getImageOrFile(item.propertyValue)"
            >
              {{ item.fileName }}
            </div>
            <!-- 其他属性 -->
            <div v-else class="content-item one-row-text">
              {{ item.propertyValue }}
            </div>
          </el-form-item>
        </el-form>
      </section>
      <section class="self-property-msg">
        <div class="msg">私有属性</div>
        <div v-if="privateProperty.length === 0" class="no-peoperty">
          暂无数据
        </div>
        <el-form
          ref="form"
          :model="form"
          label-width="280px"
          class="content"
          v-else
          label-position="left"
          label-suffix=" "
        >
          <el-form-item
            :label="`${item.propertyName}`"
            v-for="(item, index) in privateProperty"
            :key="index"
          >
            <!-- 图片或文件 -->
            <div
              v-if="item.propertyType == 'file'"
              :style="{
                color: '#6f8def',
                cursor: 'pointer',
              }"
              @click="getImageOrFile(item.propertyValue)"
            >
              {{ item.fileName }}
            </div>
            <div v-if="item.propertyType == 'image'">
              <img
                :src="item.propertyValue"
                style="width: 40px; height: auto"
                alt=""
              />
            </div>
            <!-- 其他属性 -->
            <div
              v-else
              :style="{
                color: '#333',
                cursor: 'default',
              }"
              class="content-item one-row-text"
            >
              {{ item.propertyValue }}
            </div>
          </el-form-item>
        </el-form>
      </section>
      <section class="origin-msg">
        <div class="header" style="margin-bottom: 0">
          <div class="msg">实体溯源</div>
          <div class="edit-button">
            <span>来源数量：</span><span>{{ entitySourceSize }}</span>
          </div>
        </div>
      </section>

      <el-table
        :data="tableData"
        border
        :header-cell-style="{ background: 'rgba(61, 96, 212, 0.12)' }"
        style="width: 100%"
        class="entity_table"
      >
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>

        <el-table-column prop="fileName" label="数据源名称" align="center">
        </el-table-column>

        <el-table-column
          prop="fileType"
          label="类型"
          width="160"
          align="center"
        >
        </el-table-column>

        <el-table-column
          prop="fileSize"
          label="大小(M)"
          align="center"
          width="160"
        >
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-link
              :underline="false"
              type="primary"
              class="form-action"
              :href="`${
                $api.knowledgeMap.downloadSourceFile
              }?fileUrl=${scope.row.fileUrl.replace(/\\/g, '/')}&fileName=${
                scope.row.fileName
              }`"
            >
              下载</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      propertyTypeObj: {
        text: "字符串",
        int: "整数值",
        float: "浮点值",
        date: "日期",
        datetime: "日期时间",
        coordinate: "坐标",
        array: "数组",
        image: "图片",
        file: "文件",
      },
      data: "歼击机",
      tableData: [],
      form: {
        confidence: 1,
        details: "",
        entityName: "",
        targetNumber: "",
        ontologyName: "",
        disambiguationIdentify: "",
        privatePropertyList: [],
        commonPropertyList: [],
        fileId: "",
        entitySourceSize: "",
      },
      entitySourceSize: "",
    };
  },
  computed: {
    privateProperty() {
      return this.form.privatePropertyList || [];
    },
    commonPropertyList() {
      return this.form.commonPropertyList || [];
    },
  },
  created() {
    this.getEntityDetail();
  },
  methods: {
    getImageOrFile(propertyValue) {
      window.open(propertyValue);
    },

    goBack() {
      // this.$router.go(-1);
      this.$router.push("/manage/entityManagement");
    },
    getEntityDetail() {
      let params = {
        entityId: this.$route.query.id,
      };
      this.$axios
        .get(`${this.$api.knowledgeMap.entityDetail}/${this.$route.query.id}`)
        .then((res) => {
          const { data, message, code } = res.data;
          if (code === 200) {
            if (Array.isArray(data.commonPropertyList)) {
              data.commonPropertyList = data.commonPropertyList.map((ms) => {
                Object.assign(ms, ms.ontologyProperty);
                if (ms.propertyType === "array") {
                  try {
                    ms.propertyValue = JSON.parse(ms.propertyValue);
                  } catch {
                    ms.propertyValue = [];
                  }
                }
                return ms;
              });
            }
            if (Array.isArray(data.privatePropertyList)) {
              data.entityPropertyList = data.privatePropertyList.map((es) => {
                es.propertyTypeName = this.propertyTypeObj[es.propertyType];
                return es;
              });
            }
            Object.assign(this.form, data);
            // this.form.privatePropertyList = this.form.privatePropertyList.map(
            //   (ms) => {
            //     Object.assign(ms, ms.ontologyProperty)
            //     return ms
            //   }
            // )
            this.entitySourceSize = data.entitySourceSize;
            this.tableData = data.entitySourceList;
          } else {
            this.$message.warning(message);
          }
        });
    },
    gotoEditor() {
      this.$router.push({
        path: "/manage/entityManagement/edit",
        query: {
          id: this.$route.query.id,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../../../assets/scss/scroll";

.view-entity-page {
  font-size: 14px;
  font-family: MicrosoftYaHei;
  color: #333333;
  background: #ffffff;
  border-radius: 4px;

  .view-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 56px;
    border-bottom: 1px solid #dddddd;
    padding: 0 32px;

    .title {
      font-size: 16px;
      font-weight: bold;
    }

    .btn {
      width: 77px;
      height: 32px;
    }
  }

  .property-msg,
  .self-property-msg {
    /deep/ .el-form-item__label {
      background: #f8f8f8;
      border-radius: 2px;
      border: 1px solid #dddddd;
      padding: 0 8px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-right: 16px;
    }

    /deep/ .el-form-item__content {
      margin-left: 296px !important;
    }
  }

  .no-peoperty {
    color: #bfbfbf;
    margin-bottom: 36px;
  }
}

.view-entity {
  width: 80%;
  padding: 32px;

  .content {
    .el-form-item {
      height: 32px;
      line-height: 32px;

      /deep/ .el-form-item__label {
        line-height: 32px;
      }

      /deep/ .el-form-item__content {
        line-height: 32px;
      }
      &.img-item {
        height: 70px;
        line-height: 70px;
      }
    }

    .upload {
      height: 72px;
    }

    .content-detail {
      height: 138px;

      /deep/ .el-form-item__content {
        height: 100%;
        overflow-y: auto;
        background: #f8f8f8;
        border-radius: 2px;
        border: 1px solid #dddddd;
      }
    }
  }

  .msg {
    border-left: 4px solid #4b71eb;
    padding-left: 6px;
    font-size: 16px;
    line-height: 16px;
  }

  .entity-msg,
  .origin-msg {
    .header {
      display: flex;
      justify-content: space-between;
      margin-bottom: 18px;
    }

    .edit-button {
      color: #4b71eb;
    }

    .edit-icon {
      margin-right: 6px;
      font-size: 20px;
    }
  }

  .content .content-item {
    width: 280px;
    height: 32px;
    background: #f8f8f8;
    border-radius: 2px;
    border: 1px solid #dddddd;
    padding: 0 8px;
  }

  .property-msg,
  .self-property-msg,
  .origin-msg {
    .msg {
      margin-bottom: 18px;
    }
  }

  .entity_table /deep/.cell {
    text-align: center;
  }

  .entity_table {
    /deep/ {
      .el-table__cell {
        padding: 6px 0;
      }
    }
  }
}
</style>
